import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:images_picker/images_picker.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/utils/styles.dart';
import 'package:meal/utils/tools.dart';
import 'package:scan/scan.dart';

///
/// 扫码
///
class Qrscan extends StatefulWidget {
  const Qrscan({Key? key}) : super(key: key);

  @override
  State<Qrscan> createState() => _QrscanState();
}

class _QrscanState extends State<Qrscan> {
  ScanController controller = ScanController();

  @override
  void dispose() {
    super.dispose();
    controller.pause();
  }

  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
    return Stack(
      alignment: Alignment.center,
      children: [
        ScanView(
          controller: controller,
          scanAreaScale: 0.8,
          scanLineColor: const Color(0xFF00ECD7),
          onCapture: (String data) {
            controller.resume();
            controller.pause();
            Get.back(result: data);
          },
        ),
        AppBar(
          elevation: 0,
          title: const Text(
            '二维码/条码',
            style: TextStyle(fontSize: 20),
          ),
          centerTitle: true,
          backgroundColor: Colors.transparent,
          leading: GestureDetector(
            child: Container(
              color: Colors.transparent,
              child: const Icon(
                Icons.arrow_back_ios,
                color: Colors.white,
                size: 20,
              ),
            ),
            onTap: () {
              Get.back();
            },
          ),
          actions: [
            TextButton(
                onPressed: () {
                  /// 相册
                  ToolsUtils.openGalleryImage((List<Media> res) async {
                    String? result = await Scan.parse(res[0].path);
                    controller.resume();
                    controller.pause();
                    Get.back(result: result);
                  }, 1);
                },
                child: Text(
                  '相册',
                  style: StylesUtils.customTextStyle(fontSize: Sizes.default16),
                ))
          ],
        ),
      ],
    );
  }
}
